package com.example.enterprisemanager.mvvm.fragment.finding;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.example.enterprisemanager.base.BaseViewModel;
import com.example.enterprisemanager.binding.BindingCommand;
import com.example.enterprisemanager.mvvm.adapter.GoodAdapter;
import com.example.enterprisemanager.mvvm.fragment.manager.ben.Goods;
import com.example.enterprisemanager.util.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.List;

import io.reactivex.functions.Consumer;

public class FindingViewModel extends BaseViewModel<FindModel> {
    public GoodAdapter adapter;
    public ObservableField<Boolean> rGroupTypeVisible;
    public ObservableField<Boolean> linearNameVisible;
    public ObservableField<Boolean> linearPriceVisible;
    public ObservableField<Boolean> rGroupDepartVisible;

    public BindingCommand categoryBindCommand;
    public BindingCommand guDingBindCommand;
    public BindingCommand haoCaiCommand;

    public BindingCommand nameBindCommand;
    public ObservableField<String> edtName;
    public BindingCommand nameFindCommand;

    public BindingCommand priceBindCommand;
    public ObservableField<String> edtPriceStart;
    public ObservableField<String> edtPriceEnd;
    public BindingCommand priceFindCommand;

    public BindingCommand departBindCommand;
    public BindingCommand caiGouBindCommand;
    public BindingCommand yanFaBindCommand;
    public BindingCommand houQinBindCommand;
    public BindingCommand xiaoShouBindCommand;
    public BindingCommand caiWuBindCommand;


    public BindingCommand otherBindCommand;


    public FindingViewModel(@NonNull Application application) {
        super(application, new FindModel());
        init();
    }

    private void init() {
        edtName = new ObservableField<>();
        edtPriceStart = new ObservableField<>("0");
        edtPriceEnd = new ObservableField<>("0");
        adapter = new GoodAdapter();

        rGroupTypeVisible = new ObservableField<>(true);
        linearNameVisible = new ObservableField<>(false);
        linearPriceVisible = new ObservableField<>(false);
        rGroupDepartVisible = new ObservableField<>(false);

        categoryBindCommand = new BindingCommand(() -> {
            Logger.d("categoryBindCommand");
            rGroupTypeVisible.set(true);
            linearNameVisible.set(false);
            linearPriceVisible.set(false);
            rGroupDepartVisible.set(false);

        });
        guDingBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByType("固定资产", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });

        haoCaiCommand = new BindingCommand(() -> {
            mModel.getGoodsByType("耗材", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });

        nameBindCommand = new BindingCommand(() -> {
            Logger.d("categoryBindCommand");
            rGroupTypeVisible.set(false);
            linearNameVisible.set(true);
            linearPriceVisible.set(false);
            rGroupDepartVisible.set(false);
        });
        nameFindCommand = new BindingCommand(() -> {
            String name = edtName.get();
            mModel.getGoodsByName(name, new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });

        });


        priceBindCommand = new BindingCommand(() -> {
            rGroupTypeVisible.set(false);
            linearNameVisible.set(false);
            linearPriceVisible.set(true);
            rGroupDepartVisible.set(false);

        });
        priceFindCommand = new BindingCommand(() -> {
            String start = edtPriceStart.get();
            String end = edtPriceEnd.get();

            if (start == null || end == null) {
                ToastUtil.show(getApplication(), "不可为空");
                return;
            }

            int priceStart = Integer.parseInt(start);
            int priceEnd = Integer.parseInt(end);

            if (priceStart > priceEnd) {
                ToastUtil.show(getApplication(), "数据输入有误");
                return;
            }
            Logger.e(priceStart+"**********"+priceEnd);
            mModel.getGoodsByPrice(priceStart, priceEnd, new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });

        });


        departBindCommand = new BindingCommand(() -> {
            Logger.d("categoryBindCommand");
            rGroupTypeVisible.set(false);
            linearNameVisible.set(false);
            linearPriceVisible.set(false);
            rGroupDepartVisible.set(true);

        });
        caiGouBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByDepart("采购部", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });
        yanFaBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByDepart("研发部", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });
        houQinBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByDepart("后勤部", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });
        xiaoShouBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByDepart("销售部", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });
        caiWuBindCommand = new BindingCommand(() -> {
            mModel.getGoodsByDepart("财务部", new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });


        otherBindCommand = new BindingCommand(() -> {
            Logger.d("categoryBindCommand");
            rGroupTypeVisible.set(false);
            linearNameVisible.set(false);
            linearPriceVisible.set(false);
            rGroupDepartVisible.set(false);


            mModel.getGoodsByOther(new Consumer<List<Goods>>() {
                @Override
                public void accept(List<Goods> goods) throws Exception {
                    adapter.setNewData(goods);
                    ToastUtil.show(getApplication(), "查询成功");
                }
            }, new Consumer<Exception>() {
                @Override
                public void accept(Exception e1) throws Exception {

                }
            });
        });

    }
}
